var Tools = {
	dateFormat: function(fmt, date) {
		let ret;
		const opt = {
			"Y+": date.getFullYear().toString(), // 年
			"m+": (date.getMonth() + 1).toString(), // 月
			"d+": date.getDate().toString(), // 日
			"H+": date.getHours().toString(), // 时
			"M+": date.getMinutes().toString(), // 分
			"S+": date.getSeconds().toString() // 秒
			// 有其他格式化字符需求可以继续添加，必须转化成字符串
		};
		for (let k in opt) {
			ret = new RegExp("(" + k + ")").exec(fmt);
			if (ret) {
				fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
			};
		};
		return fmt;
	},
	//解析xml
	resolveXml: function(res) {
		console.log(res);
		var data = res.data.split('<?xml version="1.0" encoding="utf-8"?>');
		var data = data[1].split('<string xmlns="http://oa.fpc98.com">');
		var data = data[1].split('</string>');
		console.log(data);
		var data = JSON.parse(data[0]);
		return data;
	},
	reslveXmlHtml: function(res) {
		// console.log($(res).find("string")[0]);
		return JSON.parse($(res).find("string")[0].innerHTML)
	},
	//解析地址栏参数
	GetRequest:function() {   
	   var url = window.location.search; //获取url中"?"符后的字串   
	   var theRequest = new Object();   
	   if (url.indexOf("?") != -1) {   
		  var str = url.substr(1);   
		  strs = str.split("&");   
		  for(var i = 0; i < strs.length; i ++) {   
			  //就是这句的问题
			 theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]); 
			 //之前用了unescape()
			 //才会出现乱码  
		  }   
	   }   
	   return theRequest;   
	},
	//导出excel
	getExcel:function(data) {
		var option = {};
		option.fileName = 'excel'
		option.datas = [{
				sheetData: data.getDatas,
				sheetName: 'sheet',
				sheetFilter: data.fieldarr,
				sheetHeader: data.titlearr
			},
			{
				sheetData: data.getDatas
			}
		];
		var toExcel = new ExportJsonExcel(option);
		console.log(toExcel)
		toExcel.saveExcel();
	},
};
var Api = {
	//获取奖品信息
	get_prize_pool: function(data) {
		console.log(data);
		return new Promise((resolve, reject) => {
			$.ajax({
				url: BASE_HOST + 'api/get/get_prize_pool',
				data,
				success(res) {
					resolve(res);
				},
				error(res) {
					console.log(res);
				}
			})
		})
	},
	//修改奖品数量
	update_prize: function(data) {
		console.log(data);
		return new Promise((resolve, reject) => {
			$.ajax({
				url: BASE_HOST + 'api/get/update_prize',
				data,
				success(res) {
					resolve(res);
				},
				error(res) {
					console.log(res);
				}
			})
		})
	},
	//获取点击量
	prize_click: function(data) {
		return new Promise((resolve, reject) => {
			$.ajax({
				url: BASE_HOST + 'api/get/prize_click',
				data,
				success(res) {
					resolve(res);
				},
				error(res) {
					console.log(res);
				}
			})
		})
	},
	//记录点击量
	insert_prize_click: function(data) {
		return new Promise((resolve, reject) => {
			$.ajax({
				url: BASE_HOST + 'api/get/insert_prize_click',
				data,
				success(res) {
					resolve(res);
				},
				error(res) {
					console.log(res);
				}
			})
		})
	},
	//抽奖记录
	prize_record: function(data) {
		return new Promise((resolve, reject) => {
			$.ajax({
				url: BASE_HOST + 'api/get/prize_record',
				data,
				success(res) {
					resolve(res);
				},
				error(res) {
					console.log(res);
				}
			})
		})
	},
	//每天每人只能抽一次奖
	exists_day_prize: function(data) {
		return new Promise((resolve, reject) => {
			$.ajax({
				url: BASE_HOST + 'api/get/exists_day_prize',
				data,
				success(res) {
					resolve(res);
				},
				error(res) {
					console.log(res);
				}
			})
		})
	},
	//用户是否存在
	exists_user: function(data) {
		return new Promise((resolve, reject) => {
			$.ajax({
				url: BASE_HOST + 'api/get/exists_user',
				data,
				success(res) {
					resolve(res);
				},
				error(res) {
					console.log(res);
				}
			})
		})
	},
	//用户验证
	loginValid: function(data) {
		return new Promise((resolve, reject) => {
			$.ajax({
				url: 'https://oa.fpc98.com/SerFile/Service.asmx/h_diary_select',
				data,
				success(res) {
					resolve(Tools.reslveXmlHtml(res));
				},
				error(res) {
					console.log(res);
				}
			})
		})
	},
	//获取参与用户
	get_user: function(data) {
		return new Promise((resolve, reject) => {
			$.ajax({
				url: BASE_HOST + 'api/get/get_user',
				success(res) {
					resolve(res);
				},
				error(res) {
					console.log(res);
				}
			})
		})
	},
	//获取抽奖信息
	get_prize: function(data) {
		return new Promise((resolve, reject) => {
			$.ajax({
				url: BASE_HOST + 'api/get/get_prize',
				data,
				success(res) {
					resolve(res);
				},
				error(res) {
					console.log(res);
				}
			})
		})
	},
	//插入用户
	insert_user: function(data) {
		return new Promise((resolve, reject) => {
			$.ajax({
				url: BASE_HOST + 'api/get/insert_user',
				data,
				success(res) {
					resolve(res);
				},
				error(res) {
					console.log(res);
				}
			})
		})
	},
	//插入抽奖记录
	insert_prize_record: function(data) {
		return new Promise((resolve, reject) => {
			$.ajax({
				url: BASE_HOST + 'api/get/insert_prize_record',
				data,
				success(res) {
					resolve(res);
				},
				error(res) {
					console.log(res);
				}
			})
		})
	}
}
